﻿using JumpstartApp.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;

namespace JumpstartApp.ViewModels
{
    using JumpstartApp.Helpers;

    public class CameraViewModel : BindableBase
    {
        public async Task<CameraViewModel> Start(Windows.UI.Xaml.Controls.CaptureElement element)
        {
            await Source.InitializeAsync();
            // TODO what if blocked?
            element.Source = this.Source;
            await Source.StartPreviewAsync();
            return this;
        }

        MediaCapture m_Source = new MediaCapture();
        public MediaCapture Source { get { return m_Source; } }

        BitmapImage m_Image = default(BitmapImage);
        public BitmapImage Image { get { return m_Image; } set { SetProperty(ref m_Image, value); } }

        StorageFile m_File = default(StorageFile);
        public StorageFile File { get { return m_File; } set { SetProperty(ref m_File, value); } }

        #region Take DelegateCommand

        DelegateCommand m_TakeCommand = null;
        public DelegateCommand TakeCommand
        {
            get
            {
                if (m_TakeCommand != null)
                    return m_TakeCommand;
                m_TakeCommand = new DelegateCommand(
                    TakeCommandExecute, TakeCommandCanExecute);
                this.PropertyChanged += (s, e) => m_TakeCommand.RaiseCanExecuteChanged();
                return m_TakeCommand;
            }
        }
        async void TakeCommandExecute()
        {
            var _Option = CreationCollisionOption.ReplaceExisting;
            File = await ApplicationData.Current.LocalFolder
                .CreateFileAsync(Guid.NewGuid().ToString(), _Option);
            var _Format = ImageEncodingProperties.CreatePng();
            await Source.CapturePhotoToStorageFileAsync(_Format, File);
            this.Image = new BitmapImage(new Uri(File.Path));
        }
        bool TakeCommandCanExecute() { return true; }

        #endregion

        #region Accept DelegateCommand

        DelegateCommand m_AcceptCommand = null;
        public DelegateCommand AcceptCommand
        {
            get
            {
                if (m_AcceptCommand != null)
                    return m_AcceptCommand;
                m_AcceptCommand = new DelegateCommand(
                    AcceptCommandExecute, AcceptCommandCanExecute);
                this.PropertyChanged += (s, e) => m_AcceptCommand.RaiseCanExecuteChanged();
                return m_AcceptCommand;
            }
        }
        async void AcceptCommandExecute()
        {
            Image = null;
            var _Option = CreationCollisionOption.ReplaceExisting;
            var _File = await ApplicationData.Current.LocalFolder
                .CreateFileAsync("Badge.png", _Option);
            await File.MoveAndReplaceAsync(_File);
            Services.Navigation.GotoHome();
        }
        bool AcceptCommandCanExecute() { return Image != null; }

        #endregion
    }
}
